package cn.cd.anStudio;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MyBusAlertActivity extends Activity {

	private Button myBusAlertConfirm = null;
	private String alertMSG = null;
	private int    alertSoundId = R.raw.alert;
	private TextView alertText = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.alert);
		
		alertMSG = this.getIntent().getStringExtra("msg");

		switch(this.getIntent().getIntExtra("type", MyBusCommonBase.LOCATION_SERVICE_ERROR)) {
		case MyBusCommonBase.LOCATION_SERVICE_ARRIVE:
			alertSoundId = R.raw.alert;
			break;
		case MyBusCommonBase.LOCATION_SERVICE_ERROR:
			alertSoundId = R.raw.error;
			break;
		}
		
		//Show text
		alertText = (TextView) findViewById(R.id.alertInfo);
		alertText.setText(alertMSG);
		
		//Background Music
		final MediaPlayer myAlertSound = MediaPlayer.create(this, alertSoundId);

		try {
			if (null != myAlertSound)
				myAlertSound.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		myAlertSound.setOnCompletionListener(new OnCompletionListener() {

			public void onCompletion(MediaPlayer arg0) {
				arg0.release();
			}
		});
		
		//User key pressed
        myBusAlertConfirm = (Button) findViewById(R.id.alertConfirm);
		
		myBusAlertConfirm.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				if (null != myAlertSound) {
					if (myAlertSound.isPlaying())
						myAlertSound.stop();
					myAlertSound.release();
				}
				finish();
			}
			
		});
	}
}
